package com.bayair.doctor.biz.im_helper;

import android.text.TextUtils;

import com.bayair.doctor.App;
import com.bayair.doctor.base.component.BaseActivity;
import com.bayair.doctor.biz.helper.SpDoctor;
import com.bayair.doctor.biz.helper.SpSetting;
import com.bayair.doctor.utils.log.KLog;
import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.TIMManager;
import com.tencent.qcloud.tim.uikit.TUIKit;
import com.tencent.qcloud.tim.uikit.base.IUIKitCallBack;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.tencent.qcloud.tim.uikit.modules.contact.ContactItemBean;
import com.tencent.qcloud.tim.uikit.utils.ToastUtil;

/**
 腾讯IM通信的通用方法
 */
public class IMHelper {

    private static final String TAG = "IMHelper";

    private IMHelper() {
    }

    private static class SingletonInstance {
        private static final IMHelper INSTANCE = new IMHelper();
    }

    public static IMHelper getInstance() {
        return SingletonInstance.INSTANCE;
    }

    //返回值为当前登录的用户名，如果是自有帐号登录，用户名与登录所传入的 identifier 相同，
    // 如果是第三方帐号，例如微信登录，QQ 登录等，登录后会有内部转换过的 identifier，后续搜索好友，入群等，
    // 都需要使用转换后的 identifier 操作。
    public static boolean isLoginIM() {
        String loginUser = TIMManager.getInstance().getLoginUser();
        KLog.i(TAG, "loginUser=" + loginUser);

        if (TextUtils.isEmpty(loginUser) || !loginUser.equals(SpDoctor.getImName())) {
            return false;
        }
        return true;
    }

    public static void loginIM(LoginListener listener) {
        String imName = SpDoctor.getImName();
        String imkey = SpDoctor.getIMSignKey();
        String serverSig = "eJw1zUELgjAYxvGvIrsW*jo3paDLDh5iQpAVeKs24yWWukZo0XdPU6--HzzPh*Ry77*0JWuPUB" +
                "-I0vsXVPrhsMQRVHV1lQ1nfKr7ua5R9RQyAMop0GQy3dZo9SCc9x1g6g7NWBPOaMRjNm" +
                "-hbXjIW52KhTytMEJ5NDtgxVbFQZGKrhRZYLB724to8qw5bMj3B-XrM0E_";
        TUIKit.login(/*"doctor1", serverSig,*/ imName, imkey, new IUIKitCallBack() {
            @Override
            public void onSuccess(Object data) {
                KLog.e(TAG, "登录成功");
                //可能需要做一些初始化操作，在loginBusiness中initIm()
                listener.onSuccess();
            }

            @Override
            public void onError(String module, int code, String desc) {
                KLog.e(TAG, "登录失败, code=" + code + ", info=" + desc);
                listener.onError();
            }
        });
    }

    /**
     检查IM登录状态
     -   如果未登录，且已经登录过
     */
    public static void checkIMLogin() {
        if (!isLoginIM() && SpSetting.isLogin()) {
            autoLoginIM(null);
        }
    }


    public static void autoLoginIM(LoginListener listener) {
        String imName = SpDoctor.getImName();
        if (TextUtils.isEmpty(imName)) {
            return;
        }
        TIMManager.getInstance().autoLogin(imName, new TIMCallBack() {
            @Override
            public void onError(int code, String desc) {
                KLog.e(TAG, "autoLogin 登录失败, code=" + code + ", info=" + desc);
                if (listener != null)listener.onError();
            }

            @Override
            public void onSuccess() {
                KLog.e(TAG, "登录成功");
                if (listener != null)listener.onSuccess();
            }
        });
    }

    public static void logout(LogOutListener listener) {
        TIMManager.getInstance().logout(new TIMCallBack() {
            @Override
            public void onError(int code, String desc) {
                ToastUtil.toastLongMessage("logout fail: " + code + "=" + desc);
                logout();
                listener.onError();
            }

            @Override
            public void onSuccess() {
                logout();
                listener.onSuccess();
            }

            private void logout() {
                BaseActivity.logout(App.getContext());
                TUIKit.unInit();
            }
        });
    }


    //登陆操作监听
    public interface LoginListener {
        void onError();

        void onSuccess();
    }

    //登出操作监听
    public interface LogOutListener {
        void onError();

        void onSuccess();
    }


    /**
     IM 联系人bean转聊天信息

     @param info
     @return
     */
    public static ChatInfo contactToChat(ContactItemBean info) {
        ChatInfo chatInfo = new ChatInfo();
        chatInfo.setType(TIMConversationType.C2C);
        chatInfo.setId(info.getId());
        String chatName = info.getId();
        if (!TextUtils.isEmpty(info.getRemark())) {
            chatName = info.getRemark();
        } else if (!TextUtils.isEmpty(info.getNickname())) {
            chatName = info.getNickname();
        }
        chatInfo.setChatName(chatName);
        return chatInfo;
    }

}

